package com.example.kaptcha.controller;

import com.example.kaptcha.KaptchaCode;
import com.example.kaptcha.adapter.Kaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 图片验证码（支持算术形式）
 *
 * @author 李振
 * @date 2019/9/27
 */
@Controller
@RequestMapping("/captcha")
public class SysCaptchaController {
    @Resource
    private Kaptcha kaptcha;

    /**
     * 验证码生成
     */
    @GetMapping(value = "/captchaImage")
    public void getKaptchaImage(HttpServletResponse response) {
        String key = kaptcha.writeImageCode(response);
        System.out.println(key);
    }


    /**
     * 验证码生成
     *
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/captchaImageBase64")
    public KaptchaCode getKaptchaBase64Image() {
        return kaptcha.writeBase64Code();
    }

    /**
     * 校验验证码
     */
    @ResponseBody
    @GetMapping(value = "/validate")
    public boolean validate(String key, String code) {
        return kaptcha.validate(key, code);
    }
}